package com.cabal.war.service.impl;

import com.cabal.auth.dao.CabalAuthMapper;
import com.cabal.auth.dao.CabalGatewayGameConfigMapper;
import com.cabal.auth.entity.CabalGatewayGameConfig;
import com.cabal.auth.vo.CabalAuthVo;
import com.cabal.common.constants.SystemConstants;
import com.cabal.common.util.DateApiUtil;
import com.cabal.config.DynamicDataSourceContextHolder;
import com.cabal.contribute.entity.CabalContributePointAccount;
import com.cabal.contribute.entity.CabalContributePointLog;
import com.cabal.contribute.service.CabalContributePointService;
import com.cabal.war.entity.CabalWarRewardItem;
import com.cabal.war.service.WarService;
import com.cabal.war.dao.WarMapper;
import com.cabal.war.entity.CabalLordOfWarPoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class WarServiceImpl implements WarService {

    @Resource
    private WarMapper warMapper;

    @Value("${war-alz-limit:1000000000}")
    private String warAlzLimit;

    @Value("${war-average-alz:2000000}")
    private String warAverageAlz;

    @Value("${war-point-min:0}")
    private Integer warPointMin;

    @Resource
    private CabalContributePointService cabalContributePointService;

    @Resource
    private CabalAuthMapper cabalAuthMapper;

    @Override
    public void warSendReward() {
        // 1、查询今天有没有已经结束的国战记录
        String todayDate = DateApiUtil.format(new Date(), DateApiUtil.NORM_DATE_PATTERN);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 20);
        calendar.set(Calendar.MINUTE, 30);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);

        Date compareTime = calendar.getTime();
        Date nowTime = new Date();

        // 这里要分两次国战时间不同来count
        // 当前时间如果是大于21:00，这说明是晚上场，需要有两次国战统计才说明有新的国战，反之如果小于21:00则说明是下午场
        int countResult = warMapper.countWarResultRecord(todayDate);
        if(countResult < 1) {
            return;
        }
        /*if(nowTime.before(compareTime)) {
            if(countResult == 0) {
                log.info("【国战】今天下午还没有结束的国战，直接结束任务...");
                return;
            }
        } else {
            if(countResult < 2) {
                log.info("【国战】今天晚上还没有结束的国战，直接结束任务...");
                return;
            }
        }*/

        // 2、查询待发送国战奖励的记录数，如果为0则直接返回结束
        int count = warMapper.countWarSendRecord();
        if(count == 0) {
            return;
        }

        // 2、查询待推送记录集合SystemConstants
        List<CabalLordOfWarPoint> cabalLordOfWarPointList = warMapper.searchWarSendRecordList();

        // 自定义奖励，查数据库配置表
        for(CabalLordOfWarPoint cabalLordOfWarPoint : cabalLordOfWarPointList) {
            Integer userNum = cabalLordOfWarPoint.getCharacterIdx() / 8;
            Integer warPoint = cabalLordOfWarPoint.getWarPoint();
            DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_ACCOUNT);
            String account = cabalAuthMapper.getCabalAuthVoByUserNum(userNum).getUserId();

            List<String> accountList = cabalAuthMapper.searchAccountList();
            boolean isVipAccount = accountList.stream().anyMatch(element -> element.equals(account));
            Integer contributePoint;
            if(isVipAccount) {
                if(warPoint == 0) {
                    continue;
                }
                contributePoint = 300;
            } else {
                // 国战积分少于100分没有奖励
                if(warPoint < warPointMin) {
                    continue;
                }

                // 计算贡献积分 1、新增贡献积分日志  2、修改贡献积分
                contributePoint = 150 + (warPoint - 100) / 10;
                if(contributePoint > 300) {
                    contributePoint = 300;
                }
            }

            CabalContributePointLog cabalContributePointLog = new CabalContributePointLog();
            cabalContributePointLog.setUserNum(userNum);
            cabalContributePointLog.setChangePoint(contributePoint);
            cabalContributePointLog.setEventType(1);
            cabalContributePointLog.setAccount(account);
            cabalContributePointService.insertCabalContributePointLog(cabalContributePointLog);

            CabalContributePointAccount cabalContributePointAccount = new CabalContributePointAccount();
            cabalContributePointAccount.setUserNum(userNum);
            cabalContributePointAccount.setContributePoint(contributePoint);
            cabalContributePointAccount.setAccount(account);
            cabalContributePointService.insertOrUpdateCabalContributePointAccount(cabalContributePointAccount);
        }

        // 3、批量更新国战记录状态为1
        warMapper.batchUpdateSendStatus();
        DynamicDataSourceContextHolder.removeContextKey();
    }

}
